#!/bin/bash
#2018年7月29日15:54:58
#auto manager linux user
#by author www.jfedu.net
########################
USR="$*"
if [ $UID -ne 0 ];then
	echo -e "\033[32m-----------------\033[0m"
	echo -e "\033[32mThe script must be executed using the root user.\033[0m"
	exit 1
fi
add_user(){
	read -p "Please enter the user name you need to create? " USR
	for USR in $USR
	do
		id $USR
		if [ $? -ne 0 ];then
			useradd -s /bin/bash $USR -d /home/$USR
			echo ${USR}_123456|passwd --stdin $USR
			if [ $? -eq 0 ];then
				echo -e "\033[32m-----------------\033[0m"
				echo -e "\033[32mThe $USR user created successfully\033[0m"
				echo -e "User,Password"
				echo -e "$USR,${USR}_123"
				echo
				tail -n 5 /etc/passwd
			fi
		else
			echo -e "\033[32m-----------------\033[0m"
			echo -e "\033[32mThis $USR user already exists, please exit\033[0m"
			exit 1
		fi
	done
}
add_user_list(){
	read -p "Please enter the list file for batch creation of users,example list.txt: " USR
	if [ ! -z $USR ];then
		for USR in $(cat $USR) 
		do
			useradd -s /bin/bash $USR -d /home/$USR
			echo ${USR}_123456|passwd --stdin $USR
			if [ $? -eq 0 ];then
				echo -e "\033[32m-----------------\033[0m"
				echo -e "\033[32mThe $USR user created successfully\033[0m"
				echo -e "User,Password"
				echo -e "$USR,${USR}_123"
				echo
				tail -n 5 /etc/passwd
			fi
		done
		
	else
		echo -e "\033[32m-----------------\033[0m"
		echo -e "\033[32mThe user list file must be entered. The reference content format is as follows:\033[0m"
		echo "jfedu1"
		echo "jfedu2"
		echo "jfedu3"
		echo "jfedu4"
		echo "......"
	fi
}

remove_user(){
        for USR in $USR
        do
		userdel -r $USR
		groupdel $USR
                if [ $? -eq 0 ];then
                        echo -e "\033[32m-----------------\033[0m"
                        echo -e "\033[32mThe $USR user delete successfully\033[0m"
                        echo
                        tail -n 5 /etc/passwd
                fi
        done
}

remove_group(){
        for USR in $USR
        do
		groupdel $USR
                if [ $? -eq 0 ];then
                        echo -e "\033[32m-----------------\033[0m"
                        echo -e "\033[32mThe $USR group delete successfully\033[0m"
                        echo
                        tail -n 5 /etc/passwd
		else	
			grep "$USR" /etc/group
			if [ $? -eq 0 ];then
				echo -e "\033[32m-----------------\033[0m"
				echo -e "\033[32mThe $USR group delete falied,cannot remove the primary group of user $USR\033[0m"
				read -p "Are you sure you want to delete the $USR user? yes or no " INPUT
				if [ $INPUT == "y" -o $INPUT == "Y" -o $INPUT == "yes" -o $INPUT == "YES" ];then
					userdel -r $USR
					groupdel $USR >>/dev/null 2>&1
					echo -e "\033[32m-----------------\033[0m"
					echo -e "\033[32mThe $USR user delete successfully\033[0m"
					echo -e "\033[32mThe $USR group delete successfully\033[0m"
				fi
			fi
                fi
        done
}

change_user_passwd(){
	read -p "Please enter your user name and new password: username password: " INPUT
	if [ `echo $INPUT|sed 's/ /\n/g'|wc -l` -eq 2 ];then
		USR=`echo $INPUT|awk '{print $1}'`
		PAS=`echo $INPUT|awk '{print $2}'`
        	for USR in $USR
        	do
			echo $PAS|passwd --stdin $USR		
        	        if [ $? -eq 0 ];then
        	                echo -e "\033[32m-----------------\033[0m"
				echo -e "\033[32mThe password of $USR user was modified successfully\033[0m"
	                        echo -e "User,Password"
	                        echo -e "$USR,$PAS"
        	                echo
        	        fi
        	done
	fi
}

case $1 in 
	1)
	add_user
	;;
	2)
	add_user_list
	;;
	3)
	remove_user
	;;
	4)
	remove_group
	;;
	5)
	change_user_passwd
	;;
	*)
	echo "---------------------------------------------"
	echo -e "\033[34mWelcome to system user management scripts:\033[0m"
	echo -e "\033[32m1) add user\033[0m"
	echo -e "\033[32m2) add_user_list\033[0m"
	echo -e "\033[32m3) remove_user\033[0m"
	echo -e "\033[32m4) remove_group\033[0m"
	echo -e "\033[32m5) change_user_passwd\033[0m"
	echo -e "\033[32mUsage:{/bin/sh $0 1|2|3|4|5|help}\033[0m"
	echo "---------------------------------------------"
esac
